package com.superzqb.asus.superzqbActivity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbActivity.Bean.LoginBean;
import com.superzqb.asus.Api.UsersApi;
import com.superzqb.asus.superzqbUtil.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Login extends Activity {
	String n;
	private Button btnLogin;
	private TextView btnReg;
	EditText user,pwd;
	private SharedPreferences sp;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
		initView();
		readSp(sp.getString("name",null));
		user.setSelection(user.getText().length());

    }
	private void initView() {
		btnReg=(TextView)findViewById(R.id.reg);
		sp=getSharedPreferences("uinfo", Context.MODE_PRIVATE);
		btnLogin=(Button)findViewById(R.id.btnLogin);
		user=(EditText)findViewById(R.id.user);
		pwd=(EditText)findViewById(R.id.pwd);
		btnReg.setOnClickListener(listen);
		btnLogin.setOnClickListener(listen);
	}

	OnClickListener listen= new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.btnLogin:
					getInfo(n=user.getText().toString(),pwd.getText().toString());
					break;
				case R.id.reg:
					Intent intent=new Intent();
					intent.setClass(Login.this,Register.class);
					startActivity(intent);
					break;
				default:
					break;
			}

		}

		private void getInfo(String s, String s1) {
			Retrofit retrofit= new Retrofit.Builder()
					.addConverterFactory(GsonConverterFactory.create())
					.baseUrl(new Url().getBase_url())
					.build();
			UsersApi api=retrofit.create(UsersApi.class);
			Call<LoginBean> call=api.getLogin(s,s1);
			call.enqueue(new Callback<LoginBean>() {
				@Override
				public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
					if(response.isSuccessful()){
						LoginBean info=response.body();
						if(info.getU_id().equals("0") )
							Toast.makeText(Login.this,"登陆失败,请重新登录",Toast.LENGTH_SHORT).show();
						else{
							saveSp(info.getU_id(),info.getFlag(),n);
							finish();
							Toast.makeText(Login.this,"登陆成功！",Toast.LENGTH_SHORT).show();
						}
					}
				}
				@Override
				public void onFailure(Call<LoginBean> call, Throwable throwable) {
					Toast.makeText(Login.this,"登陆失败,请重新登录",Toast.LENGTH_SHORT).show();
				}
			});
		}
	};
	private void readSp(String name) {
		user.setText(name);
	}
	private void saveSp(String u_id, String flag,String s) {
		SharedPreferences.Editor edit=sp.edit();
		edit.putString("uid",u_id);
		edit.putString("flag",flag);
		edit.putString("name",s);
		edit.apply();
	}
}
